package com.huaxiafinance.management.controller;

import com.huaxiafinance.management.dto.request.AddScoreProDto;
import com.huaxiafinance.management.dto.request.IntegralRecommendDto;
import com.huaxiafinance.management.dto.request.QueryShoppingProDto;
import com.huaxiafinance.management.dto.response.QueryScoreProDto;
import com.huaxiafinance.management.model.MallRecommend;
import com.huaxiafinance.management.service.YfqShoppingProService;
import com.huaxiafinance.management.util.MessageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.HashSet;
import java.util.List;
import java.util.Map;


/**
 * Created by xieWei on 2017/9/4.
 */
@Controller("yfqIntegralProController")
@RequestMapping("/shoppingPro")
public class YfqIntegralProController {

  @Autowired
  private YfqShoppingProService yfqShoppingProService;
  private static final Logger log = Logger.getLogger(YfqIntegralProController.class);

  /**
   * 商品列表
   */
  @RequestMapping(value = "/queryList", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryList(@ModelAttribute("dto") QueryShoppingProDto dto) {
    MessageResult messageResult = new MessageResult();
    try {
      List<Map> list = yfqShoppingProService.queryList(dto);
      messageResult.setData(list);
      messageResult.setRows(yfqShoppingProService.countList(dto));
      messageResult.setSuccessResult();
    } catch (Exception e) {
      log.error("系统异常", e);
      messageResult.setFailureResult("系统异常");
    }
    return messageResult.getAsJSON();
  }

  /**
   * 上下架
   */
  @RequestMapping(value = "/up", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String upDown(Integer id, Integer isGround) {
    MessageResult messageResult = new MessageResult();
    try {
      yfqShoppingProService.upDown(id, isGround);
      messageResult.setSuccessResult();
    } catch (Exception e) {
      log.error("系统异常", e);
      messageResult.setFailureResult("系统异常");
    }
    return messageResult.getAsJSON();
  }

  /**
   * 类别列表
   */
  @RequestMapping(value = "/type", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String getType() {
    MessageResult messageResult = new MessageResult();
    try {
      messageResult.setData(yfqShoppingProService.selectType());
      messageResult.setSuccessResult();
    } catch (Exception e) {
      log.error("系统异常", e);
      messageResult.setFailureResult("系统异常");
    }
    return messageResult.getAsJSON();
  }

  /**
   * 储存推荐
   */
  @RequestMapping(value = "/putRec", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String putRec(@RequestBody IntegralRecommendDto dto) {
    MessageResult messageResult = new MessageResult();
    try {
      //判断重复code
      int index = 0;
      HashSet set = new HashSet();
      for (MallRecommend rc : dto.getRecomend()) {
        if (rc.getProCode() != null && !"".equals(rc.getProCode())) {
          index += 1;
          set.add(rc.getProCode());
        }
      }
      if (index != set.size()) {

        messageResult.setFailureResult("存在重复商品");

      } else {
        yfqShoppingProService.putRec(dto.getRecomend());
        messageResult.setSuccessResult();
      }
    } catch (Exception e) {
      log.error("系统异常", e);
      messageResult.setFailureResult("系统异常");
    }
    return messageResult.getAsJSON();
  }

  /**
   * 查询推荐列表
   */
  @RequestMapping(value = "/queryRec", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryRec(String code) {
    MessageResult messageResult = new MessageResult();
    try {
      messageResult.setData(yfqShoppingProService.queryByCode(code));
      messageResult.setSuccessResult();
    } catch (Exception e) {
      log.error("系统异常", e);
      messageResult.setFailureResult("系统异常");
    }
    return messageResult.getAsJSON();
  }

  /**
   * 推荐位列表
   */
  @RequestMapping(value = "/queryRecList", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryRecList() {
    MessageResult messageResult = new MessageResult();
    try {
      messageResult.setData(yfqShoppingProService.selectAll());
      messageResult.setSuccessResult();
    } catch (Exception e) {
      log.error("系统异常", e);
      messageResult.setFailureResult("系统异常");
    }
    return messageResult.getAsJSON();
  }

  /**
   * 添加商品
   */
  @RequestMapping(value = "/addPro", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String addPro(@Valid @RequestBody AddScoreProDto dto, BindingResult bindingResult) {
    MessageResult messageResult = new MessageResult();
    try {
      if (bindingResult.hasErrors()) {
        messageResult.setFailureResult(bindingResult);
        return messageResult.getAsJSON();
      }
      int r = yfqShoppingProService.addPro(dto);
      if (r == 2) {
        messageResult.setFailureResult("商品参数不能少于两条");
      } else if (r == 3) {
        messageResult.setFailureResult("规格不能为空");
      } else if (r == 4) {
        messageResult.setFailureResult("价格不能为空");
      } else if (r == 5) {
        messageResult.setFailureResult("商品名字重复");
      } else if (r == 6) {
        messageResult.setFailureResult("sku最少一个完整的");
      } else {
        messageResult.setSuccessResult();
      }
    } catch (Exception e) {
      log.error("系统异常", e);
      messageResult.setFailureResult("系统异常");
    }
    return messageResult.getAsJSON();
  }

  /**
   * 查询标签列表
   */
  @RequestMapping(value = "/selectTag", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String selectTag(Integer category) {
    MessageResult messageResult = new MessageResult();
    try {
      messageResult.setData(yfqShoppingProService.selectTag(category));
      messageResult.setSuccessResult();
    } catch (Exception e) {
      log.error("系统异常", e);
      messageResult.setFailureResult("系统异常");
    }
    return messageResult.getAsJSON();
  }

  /**
   * 查询商品详情
   *
   * @param id 商品id
   * @return 商品信息
   */
  @RequestMapping(value = "/selectOne", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String selectOne(Integer id) {
    MessageResult messageResult = new MessageResult();
    try {
      QueryScoreProDto info = yfqShoppingProService.getProOne(id);
      messageResult.setData(info);
      messageResult.setSuccessResult();
    } catch (Exception e) {
      log.error("系统异常", e);
      messageResult.setFailureResult("系统异常");
    }
    return messageResult.getAsJSON();
  }

  /**
   * 编辑商品
   */
  @RequestMapping(value = "/updatePro", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updatePro(@Valid @RequestBody AddScoreProDto dto, BindingResult bindingResult) {
    MessageResult messageResult = new MessageResult();
    try {
      if (bindingResult.hasErrors()) {
        messageResult.setFailureResult(bindingResult);
        return messageResult.getAsJSON();
      }
      int r = yfqShoppingProService.updatePro(dto);
      if (r == 2) {
        messageResult.setFailureResult("参数不能少于两条");
      } else if (r == 5) {
        messageResult.setFailureResult("商品价格不能为空");
      } else if (r == 6) {
        messageResult.setFailureResult("商品规格不能为空");
      } else if (r == 7) {
        messageResult.setFailureResult("商品名字重复");
      } else if (r == 8) {
        messageResult.setFailureResult("sku至少一条完整");
      } else {
        messageResult.setSuccessResult();
      }
    } catch (Exception e) {
      log.error("系统异常", e);
      messageResult.setFailureResult("系统异常");
    }
    return messageResult.getAsJSON();
  }

  /**
   * 逻辑删除商品
   */
  @RequestMapping(value = "/deletePro", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String deleteOne(Integer id) {
    MessageResult messageResult = new MessageResult();
    try {
      yfqShoppingProService.deletePro(id);
      messageResult.setSuccessResult();
    } catch (Exception e) {
      messageResult.setFailureResult("系统异常");
    }
    return messageResult.getAsJSON();
  }

}
